package com.spring.component.framework.dao.util;

import java.io.Serializable;
import java.util.List;

public class Page<T> implements Serializable {

    private static final long serialVersionUID = -8717729377887861576L;

    /*** 每页记录数，默认为10 */
    protected int pageSize = 10;
    /*** 总记录数 */
    protected long total;
    /*** 当页的起始索引，从1开始 */
    protected int start = 1;
    /*** 当前页中的数据结果集 */
    protected List<T> rows;

    public Page() {
    }

    public Page(int start, long total, List<T> rows) {
        this.start = start;
        this.total = total;
        this.rows = rows;
    }

    public Page(int start, long total, int pageSize, List<T> rows) {
        this.start = start;
        this.total = total;
        this.pageSize = pageSize;
        this.rows = rows;
    }

    /**
     * 获得每页记录数
     *
     * @return
     */
    public int getPageSize() {
        return this.pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public long getTotal() {
        return total;
    }

    public void setTotal(long total) {
        this.total = total;
    }

    public int getStart() {
        return start;
    }

    public void setStart(int start) {
        this.start = start;
    }

    public List<T> getRows() {
        return rows;
    }

    public void setRows(List<T> rows) {
        this.rows = rows;
    }

    /**
     * 获取当前页
     *
     * @return
     */
    public int getCurrentPage() {
        return this.start / this.pageSize + 1;
    }

    /**
     * 获取总页数
     *
     * @return
     */
    public int getPageCount() {
        if (this.total % this.pageSize == 0) {
            return (int) (this.total / this.pageSize);
        }
        return (int) (this.total / this.pageSize) + 1;
    }

    /**
     * 获取最后一页
     *
     * @return
     */
    public int getLastPage() {
        return getPageCount();
    }

    /**
     * 是否有下一页
     *
     * @return
     */
    public boolean isHasNextPage() {
        return this.getCurrentPage() < this.getPageCount();
    }

}
